#ifndef STRUCTURES_H_
#define STRUCTURES_H_

#include "BasicConfig.h"

#define _CARATTERI_P_ \
         QString( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./%*\"+")
				 
				 
#define _DEFAULT_COLOR_ATT_ICON_ \
         QString( "#3456a2")




/* quadrato dei loghini */
#define _ALTEZZA_PICO_ 20



class AttCaseLine
{
    public:
    enum { MAGICNUMBER = 0xFFAABBBA, VERSION = 2 };
    AttCaseLine()  {
    it = "Descrizione italiano";
    de = "Descrizione deutsch";
    fr = "Descrizione francaise";
    en = "Descrizione english";
    information = "0";   /* numero remoto */
    charnummer = 0;    /* numero mio */
    cate = 0;
    data = "0";
    cate = 0;
    }
    QListWidgetItem *ListItem() {
        
       QListWidgetItem * item = new QListWidgetItem( QIcon(pico.pix()),QString("Des. ")+it);
                         item->setToolTip(QString("Nr.%1 ").arg(charnummer)+information);
                         ////////item->setData ( int role, const QVariant & value )  
        return item;
    }
    
    AttCaseLine& operator=( const AttCaseLine& d )
    {
      it = d.it;
      de = d.de;
      fr = d.fr;
      en = d.en;
      information = d.information;
      charnummer = d.charnummer;
      sign = d.sign;
      data = d.data;
      cate = d.cate;
      pico = d.pico;
      return *this;
    }
    bool operator!=( const AttCaseLine& d )
    {
        if (charnummer != d.charnummer) {
        return false;
        } else {
        return true;
        }
    }
    operator QVariant() const {
    return QVariant::fromValue(*this);
    }
    /* data to insert on dbs */
    QString sqlweb()
	  {
         QString sqlpaint =  QString("REPLACE INTO `WEB_ICONE_ATTRIBUTI` VALUES (%1,'%2');")
        .arg(charnummer)
        .arg(pico.web());
       return sqlpaint;  
    }

    /* vars permanent */
    QString it;
    QString de;
    QString fr;
    QString en;
    QString information;
    uint charnummer;
    QChar sign;
    QString data;
    int cate;
    SPics pico;   /* 10 x */
};


Q_DECLARE_METATYPE(AttCaseLine);

inline QDebug operator<<(QDebug debug, AttCaseLine& udoc)
{
	debug.nospace() << "SPics(name." 
		<< udoc.data << "," 
    << udoc.it << ","
    << udoc.information << ","
    << udoc.charnummer << ","
    << udoc.sign << ","
    << udoc.cate << ","
    << udoc.pico << ")";
	return debug.space();
}


inline QDataStream& operator<<(QDataStream& out, const AttCaseLine& udoc)
{
    out << udoc.it;
    out << udoc.de;
    out << udoc.fr;
    out << udoc.en;
    out << udoc.information;
    out << udoc.charnummer;
    out << udoc.sign;
    out << udoc.data;
    out << udoc.cate;
    out << udoc.pico;
    return out;
}

inline QDataStream& operator>>(QDataStream& in, AttCaseLine& udoc)
{
    in >> udoc.it;
    in >> udoc.de;
    in >> udoc.fr;
    in >> udoc.en;
    in >> udoc.information;
    in >> udoc.charnummer;
    in >> udoc.sign;
    in >> udoc.data;
    in >> udoc.cate;
    in >> udoc.pico;
    return in;
}

class TabellaCodici
{
    public:
    enum { MAGICNUMBER = 0xFFAABBFB, VERSION = 3 };
    TabellaCodici()
    {
    creato = QDateTime::currentDateTime();
    autore = QString(_DESCI_APP_NAME_);
    titolo = "Senza titolo";
    destcat = 0;
    }
    TabellaCodici operator=( TabellaCodici d )
    {
      all_lines = d.list();
      creato = d.creato;
      autore = d.autore;
      titolo = d.titolo;
      destcat = d.destcat;
      QList<AttCaseLine> lin;
      clear();
         for (int i=0; i<lin.size(); i++) {
		         AttCaseLine placehere = lin[i];
             append(placehere);
         }
      return *this;
    }
    bool operator!=( const TabellaCodici& d )
    {
        if (creato.toTime_t() != d.creato.toTime_t() && destcat != d.destcat ) {
        return false;
        } else {
        return true;
        }
    }
    operator QVariant() const {
    return QVariant::fromValue(*this);
    }
    void clear()
    {
      all_lines.clear();
    }
    void append( const AttCaseLine d ) 
    {
      all_lines.append(d);
    }
    QList<AttCaseLine> list()
    {
      return all_lines;
    }
    int size()
    {
      return all_lines.size();
    }
    void setlist(QList<AttCaseLine> li )
    {
        all_lines.clear();
        for (int i=0; i<li.size(); i++) {
        append(li[i]);
        }
    }
    void insert( int row , AttCaseLine mods )
    {
        all_lines.insert(row,mods);
    }
    
    QListWidget *GLista( int catecoria = 0 , QWidget * parent = 0)
    {
      QListWidget *lista = new QListWidget(parent);
        
      qDebug() << "### size  " << all_lines.size();
        
        int rows = -1;
        
		  for (int i=0; i<all_lines.size(); i++) {
				  AttCaseLine Olinea = all_lines[i];
          if ( Olinea.cate == catecoria) {
				  rows++;
				  QListWidgetItem *Cisi = Olinea.ListItem();
				  Cisi->setData(Qt::UserRole,Olinea);
				  lista->insertItem (rows,Cisi);
          }
			}
      lista->setIconSize ( QSize(80,80));
      return lista;
    }
    
    
    QList<AttCaseLine> all_lines;
     /* vars permanent */
    QDateTime creato;
    QString autore;
    QString titolo;
    int destcat;
};

Q_DECLARE_METATYPE(TabellaCodici);

inline QDebug operator<<(QDebug debug, TabellaCodici& udoc)
{
	debug.nospace() << "TabellaCodici(name()" 
		<< udoc.titolo << ",creato()" 
    << udoc.creato << ","
    << udoc.autore << ","
    << udoc.destcat << ",line_size()"
    << udoc.all_lines.size() << ")";
	return debug.space();
}

/* salvare i codici delle immagini attributi case hotel ecc... */
inline QString saveAsBuffer( TabellaCodici& data  )
{
	QByteArray bytes;
	QBuffer buffer(&bytes);
	if (!buffer.open(QIODevice::WriteOnly)) {
		return QString();
	}
	QDataStream ds(&buffer);
	/* place header */
	ds.setVersion(QDataStream::Qt_4_0);
	ds << (quint32)TabellaCodici::MAGICNUMBER;
	ds << (quint32)TabellaCodici::VERSION;
	/* place header */
  
  /*
    QDateTime creato;
    QString autore;
    QString titolo;
    int destcat;
  */

	QList<AttCaseLine> lines = data.list();
	ds << data.creato;
	ds << data.autore;
	ds << data.titolo;
	ds << data.destcat;
  
	QProgressDialog *progress = new QProgressDialog(0,Qt::Popup);
	progress->setLabelText (qApp->tr("Save %1 line.").arg(lines.size()));
	progress->setCancelButton(0);
	progress->setMaximum(lines.size());

	for (int i=0; i<lines.size(); i++) {
		AttCaseLine placehere = lines[i];
		ds << placehere;
		progress->setValue(i);
		qApp->processEvents();
		///////////qDebug() << "######## save line  " << i;
	}
	progress->setValue(lines.size());
	buffer.close();
	delete progress;
	return bytes.toBase64();
}



/* decoded base64 stream to put on mysql row , file or network streams */
inline TabellaCodici openTabBuffer( const QString datastream_base64   )
{
	QByteArray xcode("");;
	xcode.append(datastream_base64);
	AttCaseLine set;
	TabellaCodici data;
	quint32 magic, version;
	QByteArray bytes(QByteArray::fromBase64(xcode));   /* decoded base64 string to QByteArray */
	QBuffer buffer(&bytes);
	if (!buffer.open(QIODevice::ReadOnly)) {
		return data;
	}
  
	QDataStream ds(&buffer);
	/* place header */
	ds.setVersion(QDataStream::Qt_4_0);
	ds >> magic;
	if ( (quint32)TabellaCodici::MAGICNUMBER != magic ) {
    qDebug() << "######## MAGICNUMBER not ok  ";
		buffer.close();
		return data;
	}
	ds >> version;
	if ( (quint32)TabellaCodici::VERSION != version ) {
		qDebug() << "######## VERSION not ok  ";
		buffer.close();
		return data;
	}
	/* place header */
	ds >> data.creato;
	ds >> data.autore;
	ds >> data.titolo;
	ds >> data.destcat;
	/* fill line qlists */
	while (!ds.atEnd())  {
		ds >> set;
		data.append(set);
	}

	buffer.close();
	return data;
}














#endif /*STRUCTURES_H_*/
